
export default class WxShareUtils {
    private static instance: WxShareUtils;

    public static getInstance() {
        if (!this.instance) {
            this.instance = new WxShareUtils();
        }
        return this.instance;
    }

    private mWxPlus: ApicloudModules['wxPlus'];

    private constructor() {
        this.mWxPlus = api.require('wxPlus');
        if (!this.mWxPlus) {
            throw new Error('wxPlus 模块未引入');
        }
    }

    /**
    * 判断是否安装微信
    * 
    */
    public isInstalled() {
        return new Promise((res) => {
            this.mWxPlus.isInstalled((ret) => {
                res(ret && ret.installed);
            });
        });
    }

    /**
     * 分享文本
     */
    public shareText(params: ShareTextParams): Promise<boolean> {
        return new Promise((res) => {
            this.mWxPlus.shareText(params, (ret) => {
                res(ret.status);
            })
        });
    }

    /**
     * 分享图片
     * @param params 
     */
    public shareImage(params: ShareImageParams) {
        return new Promise((res) => {
            this.mWxPlus.shareImage(params, (ret) => {
                res(ret.status);
            })
        });
    }

    /**
     * 分享链接
     * @param params 
     */
    public shareWebpage(params: ShareWebpageParams) {
        return new Promise((res) => {
            this.mWxPlus.shareWebpage(params, (ret) => {
                res(ret.status);
            })
        });
    }

}

type Scene = 'session' | 'timeline' | 'favorite';
interface BsaeParams {
    apiKey?: string;
    scene?: Scene,
}
interface ShareTextParams extends BsaeParams {
    text: string;
}

interface ShareImageParams extends BsaeParams {
    thumb?: string;
    contentUrl: string;
}

interface ShareWebpageParams extends BsaeParams {
    title?: string;
    description?: string,
    thumb?: string,
    contentUrl: string,
}
